<template>
  <div v-loading="delLoading">
    <Search :search="search" :reset="reset" :inline="true">
      <template v-slot:body>
        <el-form-item label="退货单号" prop="returnnumber" maxlength="5">
          <el-input v-model="query.returnnumber" name="returnnumber" clearable />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="query.remarks" name="remarks" clearable />
        </el-form-item>
        <el-form-item label="状态" prop="statestate">
          <!-- {{ query.state }} -->
          <el-select v-model="query.state" placeholder="请选择" clearable>
            <el-option label="未审核" value="'未审核'" />
            <el-option label="已审核" value="'已审核'" />
          </el-select>
        </el-form-item>
        <el-form-item label="回写状态" prop="writestate">
          <el-select v-model="query.writestate" placeholder="请选择" clearable>
            <el-option label="未回写" value="0" />
            <el-option label="回写成功" value="1" />
            <el-option label="回写失败" value="2" />
          </el-select>
        </el-form-item>
      </template>
    </Search>
    <div class="table-default">
      <el-table :data="tableData" class="mt-3" v-loading="loading" border>
        <!-- <el-table-column prop="fseq" label="行号" /> -->
        <el-table-column prop="returnnumber" label="退货单号" width="200" />
        <el-table-column prop="remarks" label="备注" width="150" />
        <el-table-column prop="state" label="状态" width="90">
          <template #default="scope">
            <el-tag class="ml-2" type="warning" v-if="scope.row.state == '未审核'">{{ scope.row.state }}</el-tag>
            <el-tag class="ml-2" type="success" v-if="scope.row.state == '已审核'">{{ scope.row.state }}</el-tag>
            <el-tag class="ml-2" type="info" v-if="scope.row.state == ''">无数据</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="writestate" label="回写状态" width="110">
          <template #default="scope">
            <el-tag class="ml-2" type="warning" v-if="scope.row.writestate == '回写失败'">{{ scope.row.writestate }}</el-tag>
            <el-tag class="ml-2" type="success" v-if="scope.row.writestate == '回写成功'">{{ scope.row.writestate }}</el-tag>
            <el-tag class="ml-2" type="info" v-if="scope.row.writestate == ''">无数据</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="creator" label="创建者" width="130" />
        <el-table-column prop="modifier" label="修改者" />
        <el-table-column prop="created_at" label="修改时间" />
        <el-table-column label="操作" fixed="right" width="200">
          <template #default="scope">
            <div class="btn-box">
              <!-- <router-link :to="{ path: '/purchasing/purchasewarehousDetail', query: { id: `${scope.row.id}`, rowInfo: `${JSON.stringify(scope.row)}`,state:`${scope.row.state}` } }"> -->
              <router-link :to="{ path: '/purchasing/purchasereturnDetail', query: { id: `${scope.row.id}` } }">
                <Show />
              </router-link>
              <el-button type="warning" size="small" @click="approveHandler(scope.row.id)" v-if="scope.row.state != '已审核'" v-loading.fullscreen.lock="fullscreenLoading"
                >审核</el-button
              >
              <Destroy @click="destroy(api, scope.row.id)" style="margin-left: 0" v-if="scope.row.state != '已审核'" />
            </div>
          </template>
        </el-table-column>
      </el-table>
      <Paginate />
    </div>
    <Dialog v-model="visible" :title="title" destroy-on-close>
      <Create @close="close(reset)" :primary="id" :api="api" />
    </Dialog>
  </div>
</template>

<script lang="ts" setup>
import { computed, onMounted, toRaw, ref, nextTick } from 'vue'
import { useRouter } from 'vue-router'
import { useGetList } from '/admin/composables/curd/useGetList'
import { useDestroy } from '/admin/composables/curd/useDestroy'
import { useOpen } from '/admin/composables/curd/useOpen'
import http from '/admin/support/http'
import { ElMessage } from 'element-plus'
import {setTableHeight} from '/admin/utils/setHeight.js'
import emitter from '/admin/utils/emitter'

let router = useRouter()

const api = 'purchasing/purchasereturn'

const { data, query, search, reset, loading, toraw } = useGetList(api)
const { destroy, deleted,delLoading } = useDestroy()
const { open, close, title, visible, id } = useOpen()

const tableData = computed(() => data.value?.data)

console.log(tableData)

const writestateFormatter = {
  0: '回写失败',
  1: '回写成功',
}

const fullscreenLoading = ref(false)

const approveHandler = id => {
  fullscreenLoading.value = true

  http
    .put(`/purchasing/purchasereturnexamine/${id}`)
    .then(r => {
      fullscreenLoading.value = false

      ElMessage({
        message: r.data.message,
        type: 'success',
      })
      search()
    })
    .catch(error => {
      fullscreenLoading.value = false
    })
}

onMounted(() => {
  emitter.on('changepurchasereturnStatus', val => {
    console.log('=====监听数据',val);
    if (val.status == true) {
      search()
    }
  })
  search()
  deleted(reset)
  setTableHeight('.table-default')

})
</script>
<style>
.btn-box {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
}
.el-button--small {
  margin: 5px 0;
}
.el-table {
  height: 87%;
}
</style>
